import com.since.algorithm.search.RecursionDichotomy;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<UserName> list = new ArrayList<>();
        UserName user = new UserName(50, 15 + "");
        list.add(0, user);
        for (int i = 0; i < 5; i++) {
            int age = (int) (Math.random() * 500);
            user = new UserName(i + 1, age + "");
            list.add(user);
        }
        RecursionDichotomy<UserName> binarySearch = new RecursionDichotomy<>("age", list);
        try {
            UserName search = binarySearch.search("15");
            System.out.print("找到" + search.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

class UserName implements Comparable<UserName> {
    private int Id;
    private String age;

    public UserName(int id, String age) {
        Id = id;
        this.age = age;
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Integer.parseInt(age);
        result = prime * result + Id;
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserName other = (UserName) obj;
        if (age != other.age)
            return false;
        if (Id != other.Id)
            return false;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "UserName{" +
                "Id=" + Id +
                ", age='" + age + '\'' +
                '}';
    }

    @Override
    public int compareTo(UserName o) {
        if (Integer.parseInt(this.age) > Integer.parseInt(o.getAge())) {
            return 1;
        } else if (Integer.parseInt(this.age) < Integer.parseInt(o.getAge())) {
            return -1;
        } else {
            return 0;
        }
    }
}